import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入login组件
import Login from '../components/Login.vue'
// 导入Home组件
import Home from '../components/Home.vue'
// 导入Welcome组件
import Welcome from '../components/Welcome.vue'
// 导入User组件
import User from '../components/users/User.vue'
// 导入Rights.vue组件
import Rights from '../components/power/Rights.vue'
//导入Roles组件
import Roles from '../components/power/Roles.vue'
//导入Cate组件
import Cate from '../components/goods/Cate.vue'
//导入Params组件
import Params from '../components/goods/Params.vue'
//导入GoodsList组件
import GoodsList from '../components/goods/List.vue'
//导入Add组件
import Add from '../components/goods/Add.vue'
//导入Order组件
import Order from '../components/order/order.vue'
//导入Order组件
import Report from '../components/report/report.vue'

Vue.use(VueRouter)

const routes = [
  // 新增路由规则
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/Welcome', component: Welcome },
      // 用户列表组件
      { path: '/users', component: User },
	  //权限列表组件
	  { path: '/rights', component: Rights },
	  //用户列表组件
	  { path: '/roles', component: Roles },
		//用户列表组件
		{ path: '/categories', component: Cate },
		{ path: '/params', component: Params },
		{ path: '/goods', component: GoodsList },	
		{ path: '/goods/add', component: Add },	
		{ path: '/orders', component: Order },
		{ path: '/reports', component: Report }
		
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
// to代表你要访问的路径
// from代表你从哪个路径来
// next代表放行
router.beforeEach((to, from, next) => {
// 如果访问的路径是/login直接放行
  if (to.path === '/login') next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
